<template>
  <div>
    <van-nav-bar
      title="注册"
      right-text="去登录"
      left-arrow
      @click-left="$router.back()"
      @click-right="$router.push('/login')"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        name="phone"
        label="手机号"
        placeholder="手机号"
        :rules="[{ validator, message: '请填写手机号' }]"
      />
      <van-field
        v-model="nickname"
        name="nickname"
        label="昵称"
        placeholder="昵称"
        :rules="[{ required: true, message: '请填写昵称' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import {Reg} from "./util/axios";
import {Toast} from "vant"
export default {
  data() {
    return {
      password: "",
      phone: "",
      nickname: "",
    };
  },
  methods: {
    onSubmit(value) {
      if (this.validator(this.phone)) {
        Reg(value).then((res) => {
          if (res.code == 200) {
            //   console.log(res);
              Toast.success(res.msg);
              this.$router.push("/login")
          }
        });
      }
    },
    validator(val) {
      return /1\d{10}/.test(val);
      // console.log(/1\d{10}/.test(val));
    },
  },
};
</script>

<style>
</style>